//该文件用于创建Vuex中关键的store

import Vuex from 'vuex'
import Vue from 'vue'
//准备actions  用于响应组件中的动作
const actions ={
    jia(context,value){
        console.log("jia执行",context,value)
        context.commit("JIA",value)
    },
    jian(miniStore,value){
        console.log("jian执行",miniStore,value)
        miniStore.commit("JIA",value)
    },
    odd(miniStore,value){
        console.log("odd执行",miniStore,value)
        if(this.state.sum%2){
            miniStore.commit("ODD",value)
        }
    },
    wait(miniStore,value){
        console.log("wait执行",miniStore,value)
        setTimeout(() => {
            miniStore.commit("WAIT",value)
        }, 500);
    },
}
// 准备mutations 用于操作数据 state
const mutations ={
    JIA(state,value){
        console.log("JIA执行")
        state.sum+=value
    },
    JIAN(state,value){
        console.log("JIAN执行")
        state.sum-=value
    },
    ODD(state,value){
        console.log("ODD执行")
        state.sum+=value
    },
    WAIT(state,value){
        console.log("WAIT执行")
        state.sum+=value
    },
}
// 准备state 用于存储数据
const state={
    sum:0,
    school:"尚学堂",    
    subject:"前端"
}

//对state数据 针对逻辑复杂的进行计算 有点类似于 computed,对于commuted可以跨组件复用
const getters= {
    bigSum(state){
        return state.sum*10;
    }
}
Vue.use(Vuex)

export default new Vuex.Store({
    actions,
    mutations,
    state,
    getters,
})
